﻿using UnityEngine;
using System.Collections;

public class LegionTouchCamKeys : MonoBehaviour {

	public bool AllowMove;
	public float MoveSpeed;
	
	public bool AllowFastMove;
	public float FastMoveSpeed;
	public KeyCode FastMoveKeyCode1;
	public KeyCode FastMoveKeyCode2;
	
	public bool AllowRotate;
	public float RotateSpeed;
	
	public bool AllowZoom;
	public float ZoomSpeed;
	
	public bool AllowTilt;
	public float TiltSpeed;
	
	public KeyCode ResetKey;
	public bool IncludePositionOnReset;
	
	public bool MovementBreaksFollow;
	
	public string HorizontalInputAxis = "Horizontal";
	public string VerticalInputAxis = "Vertical";
	
	public bool RotateUsesInputAxis = false;
	public string RotateInputAxis = "KbCameraRotate";
	public KeyCode RotateLeftKey = KeyCode.Q;
	public KeyCode RotateRightKey = KeyCode.E;
	
	public bool ZoomUsesInputAxis = false;
	public string ZoomInputAxis = "KbCameraZoom";
	public KeyCode ZoomOutKey = KeyCode.Z;
	public KeyCode ZoomInKey = KeyCode.X;
	
	public bool TiltUsesInputAxis = false;
	public string TiltInputAxis = "KbCameraTilt";
	public KeyCode TiltUpKey = KeyCode.R;
	public KeyCode TiltDownKey = KeyCode.F;
	
	//
	
	private LegionTouchCam _cam;
	
	//
	
	public void Reset()
	{
		AllowMove = true;
		MoveSpeed = 20f;
		
		AllowFastMove = true;
		FastMoveSpeed = 40f;
		FastMoveKeyCode1 = KeyCode.LeftShift;
		FastMoveKeyCode2 = KeyCode.RightShift;
		
		AllowRotate = true;
		RotateSpeed = 180f;
		
		AllowZoom = true;
		ZoomSpeed = 20f;
		
		AllowTilt = true;
		TiltSpeed = 90f;
		
		ResetKey = KeyCode.C;
		IncludePositionOnReset = false;
		
		MovementBreaksFollow = true;
	}
	
	protected void Start()
	{
		_cam = gameObject.GetComponent<LegionTouchCam>();
	}
	
	protected void Update()
	{
		if (_cam == null)
			return;

		if(CamTakeSetting.Instance!=null && !CamTakeSetting.Instance.allowTouchCam)
			return;
		
		if (AllowMove && (!_cam.IsFollowing || MovementBreaksFollow))
		{
			var hasMovement = false;
			
			var speed = MoveSpeed;
			if (AllowFastMove && (Input.GetKey(FastMoveKeyCode1) || Input.GetKey(FastMoveKeyCode2)))
			{
				speed = FastMoveSpeed;
			}
			
			var h = Input.GetAxisRaw(HorizontalInputAxis);
			if (Mathf.Abs(h) > 0.001f)
			{
				hasMovement = true;
				_cam.AddToPosition(h * speed * Time.deltaTime, 0, 0);
			}
			
			var v = Input.GetAxisRaw(VerticalInputAxis);
			if (Mathf.Abs(v) > 0.001f)
			{
				hasMovement = true;
				_cam.AddToPosition(0, 0, v * speed * Time.deltaTime);
			}
			
			if (hasMovement && _cam.IsFollowing && MovementBreaksFollow)
				_cam.EndFollow();
		}
		
		//

		if (AllowRotate)
		{
			if (RotateUsesInputAxis)
			{
				var rot = Input.GetAxisRaw(RotateInputAxis);
				if (Mathf.Abs(rot) > 0.001f)
				{
					_cam.ChangeRotation();
					_cam.Rotation += rot * RotateSpeed * Time.deltaTime;
				}
			}
			else
			{
				if (Input.GetKey(RotateLeftKey))
				{
					_cam.ChangeRotation();
					_cam.Rotation += RotateSpeed * Time.deltaTime;
				}
				if (Input.GetKey(RotateRightKey))
				{
					_cam.ChangeRotation();
					_cam.Rotation -= RotateSpeed * Time.deltaTime;
				}
			}
		}
		
		if (AllowZoom)
		{
			if (ZoomUsesInputAxis)
			{
				var zoom = Input.GetAxisRaw(ZoomInputAxis);
				if (Mathf.Abs(zoom) > 0.001f)
				{
					_cam.Distance += zoom * ZoomSpeed * Time.deltaTime;
				}
			}
			else
			{
				if (Input.GetKey(ZoomOutKey))
				{
					_cam.ChangeZoom();
					float deltaZoom = ZoomSpeed * Time.deltaTime;
					float resultZoom = _cam.Distance + deltaZoom;
					if(resultZoom<= _cam.MaxDistance)
						_cam.Distance += deltaZoom;
				}
				if (Input.GetKey(ZoomInKey))
				{
					_cam.ChangeZoom();
					float deltaZoom = ZoomSpeed * Time.deltaTime;
					float resultZoom = _cam.Distance - deltaZoom;
					if(resultZoom>= _cam.MinDistance)
						_cam.Distance -= deltaZoom;
				}
			}
		}

		if (AllowTilt)
		{
			if (TiltUsesInputAxis)
			{
				var tilt = Input.GetAxisRaw(TiltInputAxis);
				if (Mathf.Abs(tilt) > 0.001f)
				{
					_cam.ChangeTilt();
					_cam.Tilt += tilt * TiltSpeed * Time.deltaTime;
				}
			}
			else
			{
				if (Input.GetKey(TiltUpKey))
				{
					_cam.ChangeTilt();
					float deltaTilt = TiltSpeed * Time.deltaTime;
					float resultTilt = _cam.Tilt + deltaTilt;
					if(resultTilt<=_cam.MaxTilt)
						_cam.Tilt += deltaTilt;
				}
				if (Input.GetKey(TiltDownKey))
				{
					_cam.ChangeTilt();
					float deltaTilt = TiltSpeed * Time.deltaTime;
					float resultTilt = _cam.Tilt - deltaTilt;
					if(resultTilt>=_cam.MinTilt)
						_cam.Tilt -= deltaTilt;
				}
			}
		}
		
		//
		/*
		if (ResetKey != KeyCode.None)
		{
			if (Input.GetKeyDown(ResetKey))
			{
				_cam.ResetToInitialValues(IncludePositionOnReset, false);
			}
		}*/
	}
}
